package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelLike;
import cn.wolfcode.trip.base.mapper.TravelLikeMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelLikeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 给攻略点赞的service
 */
@Service
public class TravelLikeServiceImpl implements ITravelLikeService {

    @Autowired
    private TravelLikeMapper travelLikeMapper;

    /**
     * 点赞保存
     * @param travelLike
     */
    @Override
    public void save(TravelLike travelLike) {
        travelLikeMapper.insert(travelLike);
    }

    /**
     * 点赞取消
     * @param travelLike
     */
    @Override
    public void updateStatus(TravelLike travelLike) {
        travelLikeMapper.updateStatus(travelLike);
    }

    /**
     * 统计某条大攻略被点赞的次数
     * @param commentId
     * @return
     */
    @Override
    public int countNumByCommentId(Long commentId) {
        int num = travelLikeMapper.countNumByCommentId(commentId);
        return num;
    }

    @Override
    public PageInfo getTravelLikerById(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Object> list = travelLikeMapper.getTravelLikerById(qo);
        return new PageInfo(list);

    }

    @Override
    public void changBrowerStatus(Long userId) {
        List<Object> list = travelLikeMapper.lookingForTravelLikeIdByUserId(userId);
        if (list!=null){
            for (Object travellikerId : list) {
                
                Long id = (Long) travellikerId;
                travelLikeMapper.changBrowerStatus(id);
            }
        }


    }

    @Override
    public int countNumBytravelId(Long userId) {
        Integer num = travelLikeMapper.countNumBytravelId(userId);
        if(num==null){
            return -1;
        }
        return num;
    }

    @Override
    public void delete(TravelLike travelLike) {
        travelLikeMapper.delete(travelLike);
    }

    /**
     * 判断当前用户是否对该大攻略点了赞
     * @param travelLike
     * @return
     */
    @Override
    public int getStatusByLikerId(TravelLike travelLike) {
        Integer status = travelLikeMapper.getStatusByLikerId(travelLike);
        if(status==null){
            return -1;
        }
        return status;
    }
}
